package stu.aistar.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import stu.aistar.mapper.MovieMapper;
import stu.aistar.mapper.MovieTypeMapper;
import stu.aistar.model.pojo.Movie;

import stu.aistar.model.pojo.MovieType;
import stu.aistar.service.IMovieService;

import java.util.List;
@Service
public class IMovieServiceImpl implements IMovieService {

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private MovieTypeMapper movieTypeMapper;

    @Override
    public List<Movie> findByConditionals(String name, String city, String area, String cname) {
        return movieMapper.findByConditionals(name,city,area,cname);
    }
    @Override
    public PageInfo<Movie> findMoviesByPage(String page,String pageSize) {
        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(pageSize));
        //  List<User> users = moiveMapper.findUsersByConditions(username,phone,name);
        List<Movie> movies=movieMapper.findAll();
        PageInfo<Movie> pageInfo = new PageInfo<Movie>(movies);
        return pageInfo;
    }

    @Override
    public Integer delMovieById(Integer id) {
        return movieMapper.delMoiveById(id);
    }

    @Override
    public Integer showMovieById(Integer id) {
        return movieMapper.showMovieById(id);
    }

    @Override
    public Integer delMoviesByIds(List<Integer> id) {
        return movieMapper.delMoviesByIds(id);
    }

    @Override
    public Integer showMoviesByIds(List<Integer> id) {
        return movieMapper.showMoviesByIds(id);
    }

    @Override
    public List<Movie> searchMovies(Movie movie) {
        return movieMapper.searchMovies(movie);
    }

    @Override
    public PageInfo<Movie> findMoviesByPage(String page, String pageSize, Movie movie) {
        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(pageSize));
        List<Movie> movies ;
        PageInfo<Movie> pageInfo;
        //if(null==movie){
        //    movies = moiveMapper.searchMovies(movie);
        //
        //    pageInfo = new PageInfo<>(movies);
        //}else {
        //    pageInfo = new PageInfo<>(moiveMapper.findAll());
        //}
        movies=movieMapper.searchMovies(movie);
        for (Movie m:movies) {
            String s = m.getMovieTypes().toString();
            String[] ids = s.substring(1,s.length()-1).split(",");
            List<MovieType> types = movieTypeMapper.findMovieTypeByIds(ids);
            String name = "";
            for (MovieType type: types) {
                name = name + type.getTypeName()+"、";
            }
            m.setTypeName(name.substring(0,name.length()-1));
        }
        pageInfo = new PageInfo<>(movies);
        return pageInfo;
    }

    @Override
    public Integer saveMovie(Movie movie) {
        return movieMapper.insertMovie(movie);
    }

    @Override
    public Movie findMovieIsExist(String name) {
        return movieMapper.findByName(name);
    }

    @Override
    public Boolean updateMovieById(Movie movie) {
        String types = "";
        List<String> movieTypes = movie.getMovieTypes();
        for (int i = 0; i < movieTypes.size(); i++) {
            types += movie.getMovieTypes().get(i)+",";
        }
        Integer row1=movieTypeMapper.updateMovieTypes(types.substring(0, types.length() - 1), movie.getId());
        Integer row2=movieMapper.updateMovieById(movie);
        return row1.equals(row2) && row1 == 1;
    }

    @Override
    public List<Movie> findByCid(Integer id) {
        return movieMapper.findByCid(id);
    }

    @Override
    public List<Movie> findAll() {
        return movieMapper.findAllMovies();
    }

    @Override
    public Movie findByMovieId(Integer id) {
        return movieMapper.findByMovieId(id);
    }
}
